<template>
	<view class="change-avatar-page">
		<!-- 自定义导航栏 -->
		<view class="nav-bar" :style="{ paddingTop: statusBarHeight + 'px' }">
			<view class="nav-content">
				<view class="nav-back" @click="goBack">
					<text class="back-icon">‹</text>
				</view>
				<text class="nav-title">信息修改</text>
				<view class="nav-right">
					<text class="nav-icon">⋯</text>
					<text class="nav-icon">○</text>
				</view>
			</view>
		</view>

		<!-- 内容区域 -->
		<view class="content-area">
			<!-- 顶部操作栏 -->
			<view class="action-bar">
				<text class="cancel-btn" @click="goBack">取消</text>
				<text class="confirm-btn" @click="confirmAvatar">确认</text>
			</view>

			<!-- 头像选择网格 -->
			<view class="avatar-grid">
				<view 
					class="avatar-item" 
					v-for="(avatar, index) in avatarList" 
					:key="index"
					@click="selectAvatar(avatar, index)"
				>
					<image :src="avatar" class="avatar-img" mode="aspectFill" />
					<view class="select-indicator" v-if="selectedIndex === index">
						<text class="indicator-dot">●</text>
					</view>
				</view>
				
				<!-- 上传照片选项 -->
				<view class="avatar-item upload-item" @click="uploadPhoto">
					<view class="upload-box">
						<text class="plus-icon">+</text>
						<text class="upload-text">上传照片</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import {
		ref
	} from 'vue';

	// 状态栏高度
	const statusBarHeight = ref(0);

	// 预设头像列表（可以根据实际需求替换）
	const avatarList = ref([
		'/static/老人4.png',
		'/static/老人4.png',
		'/static/老人4.png',
		'/static/老人4.png',
		'/static/老人4.png'
	]);

	// 选中的头像索引
	const selectedIndex = ref(-1);
	// 选中的头像URL（可能是预设头像或上传的照片）
	const selectedAvatar = ref('');

	// 返回上一页
	const goBack = () => {
		uni.navigateBack();
	};

	// 选择预设头像
	const selectAvatar = (avatar, index) => {
		selectedIndex.value = index;
		selectedAvatar.value = avatar;
	};

	// 上传照片
	const uploadPhoto = () => {
		uni.chooseImage({
			count: 1,
			sizeType: ['compressed'],
			sourceType: ['album', 'camera'],
			success: (res) => {
				const tempFilePath = res.tempFilePaths[0];
				// 将上传的照片添加到头像列表末尾，并选中它
				avatarList.value.push(tempFilePath);
				selectedAvatar.value = tempFilePath;
				selectedIndex.value = avatarList.value.length - 1; // 设置为上传照片的索引
			},
			fail: (err) => {
				console.error('选择图片失败', err);
				uni.showToast({
					title: '选择图片失败',
					icon: 'none'
				});
			}
		});
	};

	// 确认选择头像
	const confirmAvatar = () => {
		if (!selectedAvatar.value) {
			uni.showToast({
				title: '请选择头像',
				icon: 'none'
			});
			return;
		}

		// 将选中的头像存储到本地，供返回时使用
		uni.setStorageSync('updatedAvatar', selectedAvatar.value);
		uni.showToast({
			title: '头像更换成功',
			icon: 'success',
			success: () => {
				setTimeout(() => {
					uni.navigateBack();
				}, 1500);
			}
		});
	};

	onLoad((options) => {
		// 获取状态栏高度
		const systemInfo = uni.getSystemInfoSync();
		statusBarHeight.value = systemInfo.statusBarHeight;

		// 如果传入了当前头像，则选中它
		if (options.currentAvatar) {
			const currentIndex = avatarList.value.findIndex(avatar => avatar === options.currentAvatar);
			if (currentIndex !== -1) {
				selectedIndex.value = currentIndex;
				selectedAvatar.value = options.currentAvatar;
			} else {
				// 如果当前头像不在预设列表中，可能是上传的照片
				selectedAvatar.value = options.currentAvatar;
			}
		}
	});
</script>

<style scoped>
	.change-avatar-page {
		background-color: #f5f5f5;
		min-height: 100vh;
	}

	/* 导航栏 */
	.nav-bar {
		background-color: #007AFF;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		z-index: 1000;
	}

	.nav-content {
		height: 44px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 30rpx;
	}

	.nav-back {
		width: 60rpx;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.back-icon {
		color: #fff;
		font-size: 48rpx;
		font-weight: 300;
		line-height: 1;
	}

	.nav-title {
		color: #fff;
		font-size: 36rpx;
		font-weight: 500;
	}

	.nav-right {
		display: flex;
		align-items: center;
		gap: 20rpx;
	}

	.nav-icon {
		color: #fff;
		font-size: 40rpx;
	}

	/* 内容区域 */
	.content-area {
		margin-top: calc(44px + var(--status-bar-height, 0px));
		background-color: #fff;
		border-radius: 20rpx 20rpx 0 0;
		padding: 30rpx;
		margin: calc(44px + var(--status-bar-height, 0px)) 30rpx 0;
	}

	/* 顶部操作栏 */
	.action-bar {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx 0 40rpx;
	}

	.cancel-btn,
	.confirm-btn {
		font-size: 32rpx;
		color: #333;
		padding: 10rpx 20rpx;
	}

	.confirm-btn {
		color: #007AFF;
		font-weight: 500;
	}

	/* 头像选择网格 */
	.avatar-grid {
		display: flex;
		flex-wrap: wrap;
		gap: 20rpx;
		justify-content: flex-start;
	}

	.avatar-item {
		width: calc(33.333% - 14rpx);
		aspect-ratio: 1;
		position: relative;
		border-radius: 12rpx;
		overflow: hidden;
		background-color: #f5f5f5;
	}

	.avatar-img {
		width: 100%;
		height: 100%;
		border-radius: 12rpx;
	}

	.select-indicator {
		position: absolute;
		top: 8rpx;
		left: 8rpx;
		width: 40rpx;
		height: 40rpx;
		background-color: rgba(255, 255, 255, 0.9);
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.indicator-dot {
		color: #007AFF;
		font-size: 24rpx;
		line-height: 1;
	}

	/* 上传照片选项 */
	.upload-item {
		border: none;
		background-color: #f5f5f5;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.upload-box {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		gap: 20rpx;
		width: 100%;
		height: 100%;
	}

	.plus-icon {
		font-size: 80rpx;
		color: #999;
		line-height: 1;
		font-weight: 300;
	}

	.upload-text {
		font-size: 28rpx;
		color: #999;
	}
</style>
